﻿using System;
using System.Windows.Forms;
using TomatoClock.Models;
using TomatoClock.ViewModels;

namespace TomatoClock.Views
{
    public partial class TomatoTimerSetterControl : UserControl
    {
        private MainViewModel viewModel;

        public void setMainViewModel(MainViewModel viewModel)
        {
            this.viewModel = viewModel;
        }

        public TomatoTimerSetterControl(MainViewModel viewModel)
        {
            InitializeComponent();
            this.viewModel = viewModel;
            this.Dock = DockStyle.Fill;
        }

        private void set25MinButton_Click(object sender, EventArgs e)
        {
            SetTimer(25);
        }

        private void set35MinButton_Click(object sender, EventArgs e)
        {
            SetTimer(35);
        }

        private void customTimeButton_Click(object sender, EventArgs e)
        {
            using (var customTimeDialog = new CustomTimeDialog())
            {
                if (customTimeDialog.ShowDialog() == DialogResult.OK)
                {
                    int customTime = customTimeDialog.CustomTime;
                    SetTimer(customTime);
                }
            }
        }

        private void SetTimer(int duration)
        {
            string taskName = taskNameTextBox.Text.Trim();
            if (string.IsNullOrEmpty(taskName))
            {
                MessageBox.Show("请输入任务名称");
                return;
            }

            var task = new PomodoroTask
            {
                UserId=viewModel.CurrentUser.UserId,
                Name = taskName,
                Duration = duration,
                Type = "Work", // 这里可以根据需要进行更改
                StartTime = DateTime.Now
            };

            viewModel.AddTask(task); // 将任务添加到ViewModel
            var timerControl = new TomatoTimerControl(viewModel, task, []);
            timerControl.Dock = DockStyle.Fill; // 让 UserControl 填满 Panel
            timerControl.TimerFinished += TimerControl_TimerFinished;

            // 查找包含当前控件的窗体
            Form parentForm = this.FindForm();

            // 确保找到的窗体是 MainForm
            if (parentForm is MainForm mainForm)
            {
                // 调用 ShowPage 方法来显示 TomatoTimerSetterControl
                mainForm.ShowTimerPage(new TomatoTimerControl(viewModel, task, []));
            }
        }

        private void TimerControl_TimerFinished()
        {
            // 查找包含当前控件的窗体
            Form parentForm = this.FindForm();

            // 确保找到的窗体是 MainForm
            if (parentForm is MainForm mainForm)
            {
                // 调用 ShowPage 方法来显示其他页面
                mainForm.ShowPage(new HomeControl());
            }
        }

        private void taskNameTextBox_Click(object sender, EventArgs e)
        {
            if (taskNameTextBox.Text == "请输入任务名称")
            {
                taskNameTextBox.Text = "";
                taskNameTextBox.ForeColor = SystemColors.ControlText;
            }
        }

        private void taskNameTextBox_Load(object sender, EventArgs e)
        {
            // 设置 taskNameTextBox 的提示文本
            taskNameTextBox.Text = "请输入任务名称";
            taskNameTextBox.ForeColor = Color.Gray;
        }

        private void taskNameTextBox_Enter(object sender, EventArgs e)
        {
            
        }

        private void taskNameTextBox_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(taskNameTextBox.Text))
            {
                taskNameTextBox.Text = "请输入任务名称";
                taskNameTextBox.ForeColor = Color.Gray;
            }
        }
    }
}
